package com.wx.wxapp.service;

import com.wx.wxapp.dao.*;
import com.wx.wxapp.entity.*;
import com.wx.wxapp.exception.PetException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PetService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /*
     *   功能：获取用户添加的宠物信息
     *   参数：
     *       openid:用户的openid
     * */
    @Transactional
    public Pet getPet(String openid){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        try {
            if(openid.equals("")){
                System.out.println("huanle");
                return null;
            }
            else {
                User user = userMapper.selectByOpenid(openid);
                int id = user.getId();
                Pet pet = petMapper.selectByUserId(id);
                return pet;
            }
        } finally {
            sqlSession.commit();
            sqlSession.close();
            System.out.println("guanle");
        }

    }

    /*
     *   功能：修改宠物信息
     *   参数：
     *       name:宠物名字
     *       age:宠物年龄
     *       sex:宠物性别
     *       variety:宠物种类
     *       introduction:宠物介绍
     *       openid:用户openid
     * */
    @Transactional
    public void changePetInfo(String name,int age,String sex,String variety,String introduction,String openid){
        if (name.equals("")){
            throw new PetException(PetException.NAME_EMPTY_MESSAGE,PetException.NAME_EMPTY);
        }
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int user_id = userMapper.selectByOpenid(openid).getId();
        Pet pet = petMapper.selectByUserId(user_id);
        if(pet == null){
            Pet pet1 = new Pet();
            pet1.setName(name);pet1.setAge(age);pet1.setSex(sex);pet1.setVariety(variety);pet1.setIntroduction(introduction);pet1.setHost(user_id);
            petMapper.insert(pet1);
        }else {
            pet.setName(name);pet.setAge(age);pet.setSex(sex);pet.setVariety(variety);pet.setIntroduction(introduction);pet.setHost(user_id);
            petMapper.updateByPrimaryKey(pet);
        }
        sqlSession.close();
    }

    @Transactional
    public Map getPetAndImage(int id){
        Map map = new HashMap();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
        PetimageMapper petimageMapper = sqlSession.getMapper(PetimageMapper.class);
        ActivityjoinedMapper activityjoinedMapper = sqlSession.getMapper(ActivityjoinedMapper.class);
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        Pet pet = petMapper.selectByUserId(id);
        int petId = pet.getId();
        List<Petimage> petimages = petimageMapper.selectByUserId(id);
        Activityjoined activityjoined = activityjoinedMapper.selectByPetId(petId);
        if(null != pet){
            List<String>  list = new ArrayList<>();
            System.out.println("1");
            list.add(pet.getName());list.add(pet.getSex());list.add(String.valueOf(pet.getAge()));list.add(pet.getVariety());
            map.put("info",list);
        }else {
            System.out.println("123");
        }
        if(!petimages.isEmpty()){
            List<String>  list = new ArrayList<>();
            System.out.println("2");
            Petimage petimage = petimages.iterator().next();
            list.add(petimage.getImagename());
            map.put("image",list);
        }else {
            System.out.println("234");
        }
        if(activityjoined != null){
            List<String>  list = new ArrayList<>();
            String activityId = activityjoinedMapper.selectByPetId(petId).getActivityid();
            Activity activity = activityMapper.selectByPrimaryKey(activityId);
            list.add(activity.getId());list.add(activity.getTitle());
            map.put("activity",list);
        }else {
            System.out.println("345");
        }
        sqlSession.close();
        return map;
    }

    @Transactional
    public int GetPetId(String status){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int userId = userMapper.selectByOpenid(status).getId();
        Pet pet = petMapper.selectByUserId(userId);
        sqlSession.close();
        if (pet != null) {
            return pet.getId();
        }
        else {
            return 0;
        }
    }
}
